﻿using System;
using System.Collections.Generic;
using System.Linq;

//using WebSocketSharp;

namespace BEX.Core.Exchange.MTGOXWebSocket
{
    /*
    public class MtGox_socket : Exchange
    {
        private string apiKey = "03a959bd-5e6d-4a62-88ef-89a3f9c793e2";
        private string apiSecret = "/tiV2dYBapzi4URaieEEsUj+mtYbX/ZewWGK76Y+DexAj6uw7n8cp9KE0vb21Pl4LZZAQu9KDl58cgS4hJ46zA==";

        private string Url = "http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast";

        private WebSocket[] _listeners;

        private System.Timers.Timer _refresh;


        public MtGox_socket()
            : base(ExchangeType.MTGOX)
        {
            Type = ExchangeType.MTGOX;

            _refresh = new System.Timers.Timer(30000);
            _refresh.Elapsed += _refresh_Elapsed;
            _refresh.AutoReset = true;
            _refresh.Start();
        }

        /*
        protected void _refresh_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            _refresh.Stop();


            if (null != _listeners)
            {
                var _t = Tick.GetCurrentTick(ExchangeType.MTGOX, CurrencyType.BTC, CurrencyType.USD);

                var _l = DateTime.Now - _t.Time;

                if (_l.TotalMinutes > 2)
                {
                    InvokeStatusEvent("MtGox: two minutes have expired since the last BTC/USD tick.  Refreshing.");
                    Listen();
                }
            }
            else
            {
                InvokeStatusEvent("MtGox attempted IsALive Check, but listeners object was null.");
            }
            _refresh.Start();
        }


        protected override string ReconcileCurrencyWatchList()
        {
            var res = string.Empty;



            return res;
        }


        public override Tick GetTick(CurrencyPair pair)
        {
            return null;
        }

        public override void Listen()
        {
            if (null != _listeners)
            {
                for (var i = 0; i < _listeners.Length; ++i)
                {
                    _listeners[i].Dispose();
                    _listeners[i] = null;
                }

                _listeners = null;
            }

            _listeners = new WebSocket[_toListen.Count];

            for (var i = 0; i < _toListen.Count; ++i)
            {
                if (((CurrencyType)_toListen[i]) == CurrencyType.LTC)
                {
                    continue;
                }
                _listeners[i] = new WebSocket(TickerURL + ((CurrencyType)_toListen[i]).ToString());
                _listeners[i].Origin = "http://websocket.mtgox.com";

                _listeners[i].OnClose += MtGox_OnClose;
                _listeners[i].OnError += MtGox_OnError;
                _listeners[i].OnMessage += MtGox_OnMessage;

                _listeners[i].Connect();
            }

            Report();
        }

        protected void MtGox_OnMessage(object sender, MessageEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.Data))
            {
                try
                {
                    if (e.Data.Contains("ticker.BTC"))
                    {
                        var _m = new MTGoxResponse(e.Data);

                        var _last = Tick.GetCurrentTick(ExchangeType.MTGOX, TradeCurrency.GetCurrencyType(_m.BaseCurrency), TradeCurrency.GetCurrencyType(_m.CounterCurrency));

                        if (null == _last || (_last.Bid != Convert.ToInt32(_m.ticker.sell.value_int) || _last.Ask != Convert.ToInt32(_m.ticker.buy.value_int)))
                        {
                            InvokeStatusEvent("Adding Tick (MtGox) (" + _m.BaseCurrency + "/" + _m.CounterCurrency + ") (" + _m.ticker.sell.value_int + ") (" + _m.ticker.buy.value_int + ") (" + DateTime.UtcNow.ToString() + ")");
                            CreateTickEntry(_m, TradeCurrency.GetCurrencyType(_m.BaseCurrency), TradeCurrency.GetCurrencyType(_m.CounterCurrency));
                        }
                    }
                }
                catch (Exception ex)
                {
                    InvokeStatusEvent(ex.Message);
                }
            }
            else
            {
                InvokeStatusEvent("Response was null or empty");
            }
        }

        protected void MtGox_OnError(object sender, WebSocketSharp.ErrorEventArgs e)
        {
            InvokeStatusEvent("MtGox Error: " + e.Message);
        }

        protected void MtGox_OnClose(object sender, CloseEventArgs e)
        {
            InvokeStatusEvent("MtGox connection closed.");
        }
       
    }
    */
}
